import { defineStore } from "pinia";
import { invoke } from "@tauri-apps/api/core";

export const useFolderStore = defineStore("folder", {
    state: () => {
        return {
            app_version: "0.0.1",
            showRightMenu: false, // 是否显示自定义右键菜单
            showGlobalRightMenu: false, // 是否显示全局右键菜单
            rightMenuX: 0, // 右键菜单x坐标
            rightMenuY: 0, // 右键菜单y坐标
            activePath: '', // 当前文件夹路径
            rightHoverItem: <any>{}, // 鼠标单击右键当前的文件/文件夹信息
            folderInfo: <any>[], // 文件/文件夹列表集合
        };
    },
    actions: {
        /**
         * 获取文件/文件夹信息
         * @param folderPath 文件路径
         */
        async getFolderInfo (folderPath: string, bindValue?: boolean) : Promise<any> {

            return new Promise((resolve, reject) => {
                try {
                    invoke("get_folder_info", { path: folderPath })
                    .then((info) => {
                        this.activePath = folderPath;
                        console.log('当前路径', this.activePath)
                        console.log("磁盘信息:", info);
                        if (bindValue) {
                            this.folderInfo = info;
                        }
                        resolve(info);
                    })
                    .catch((error) => {
                        console.error("[Try Error]: 获取磁盘信息失败:", error);
                        reject(error);
                    });
                } catch (error) {
                    console.error("[Catch Error]: 获取磁盘信息失败:", error);
                }
            });
        },
        /**
         * 文件夹重命名
         * @param oldPath 旧路径
         * @param newPath 新路径
         */
        async renameFolder(oldPath: string, newPath: string): Promise<void> {
            return new Promise(async (resolve, reject) => {
                try {
                  await invoke('rename_folder', { oldPath: oldPath, newPath: newPath });
                  console.log('重命名成功.');
                  resolve()
                } catch (error) {
                  console.error('重命名失败:', error);
                  reject(error)
                }
            })
        }
    }
});
